package thread;

public class test9 {
    private static int count = 0;
// 当两个线程在使用一个对象的时候,会进行竞争锁,而不同的对象则不会出现这种情况
    public static void main(String[] args) throws InterruptedException {
        Object locked=new Object();
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                synchronized (locked){
                    count++;
                }
            }
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                synchronized (locked){
                    count++;
                }
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("count = " + count);
    }
}
